''' <summary>
''' Handles displaying the drop down menu.  This only displays when there are more tabs than can fit on the screen.
''' </summary>
''' <remarks>
''' This does some strange logic to make sure that the if the drop down menu is already displayed, it doesn't re-display
''' when the button is clicked.  Also of note is the DropdownRenderer class, which makes the dropdown menu render using
''' the same color scheme as the tab control.
''' </remarks>
Friend Class DropdownButton
    Inherits TabBaseControl

#Region "Button Logic"
    Public Enum EArrowDirection
        Left = 225
        Right = 45
        Up = 315
        Down = 135
    End Enum

    Private myArrowDirection As EArrowDirection
    Public Property ArrowDirection() As EArrowDirection
        Get
            Return myArrowDirection
        End Get
        Set(ByVal value As EArrowDirection)
            myArrowDirection = value
            Me.Invalidate()
        End Set
    End Property

    Private myIsClicking As Boolean = False
    Public Property IsClicking() As Boolean
        Get
            Return myIsClicking
        End Get
        Set(ByVal value As Boolean)
            If (myIsClicking <> value) Then
                myIsClicking = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Protected Overrides ReadOnly Property IsHighlighted() As Boolean
        Get
            Return Not Me.DropdownMenu.Visible AndAlso MyBase.IsHighlighted
        End Get
    End Property

    Public Sub New(ByVal view As TabView)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)

        Me.ArrowDirection = DropdownButton.EArrowDirection.Down
        Me.Dock = System.Windows.Forms.DockStyle.Left
        Me.IsClicking = False
        Me.Location = New System.Drawing.Point(0, 0)
        Me.Size = New System.Drawing.Size(16, view.Height)
        Me.Visible = False
        view.Pages.ToolTips.SetToolTip(Me, "Show all open pages.")
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseDown(e)
        If (e.Button = Windows.Forms.MouseButtons.Left) Then Me.IsClicking = True
    End Sub

    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)
        If (e.Button = Windows.Forms.MouseButtons.Left) Then Me.IsClicking = False
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        If (Me.Pages Is Nothing) Then Exit Sub

        If (Me.IsClicking) Then
            Dim Bounds As New Rectangle(0, TopMargin, Me.Width, Me.Height - TopMargin - 1)
            Me.PaintBackground(e, Bounds, New Rectangle(Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height / 2), Pages.TabColor, AddColor(Me.Pages.TabColor, -75))
        Else
            MyBase.OnPaint(e)
        End If

        Dim size As Single = 5

        e.Graphics.TranslateTransform((Bounds.Width / 2), Me.Padding.Top + (Bounds.Height / 2))

        e.Graphics.RotateTransform(Me.ArrowDirection)

        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.None
        e.Graphics.DrawLines(New Pen(Me.ForeColor, 2), New PointF() {New PointF(-size * 0.75, -size * 0.25), New PointF(size * 0.25, -size * 0.25), New PointF(size * 0.25, size * 0.75)})
    End Sub

    Private Class DropdownMenuStrip
        Inherits ContextMenuStrip

        Public IsVisible As Boolean = False
    End Class

    Private WithEvents DropdownMenu As New DropdownMenuStrip()

    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        MyBase.OnClick(e)

        DropdownMenu.IsVisible = Not DropdownMenu.IsVisible
        If (Not DropdownMenu.IsVisible) Then Exit Sub

        Me.DropdownMenu.Items.Clear()
        For Each page As TabPage In Me.Pages
            DropdownMenu.Items.Add(page.Text).Tag = page
        Next

        DropdownMenu.Renderer = New DropdownRenderer(Me.Pages.TabColor)
        DropdownMenu.Show(Me, New Point(Me.Left, Me.Bottom), ToolStripDropDownDirection.BelowRight)
    End Sub

    Private Sub DropdownMenu_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles DropdownMenu.ItemClicked
        Pages.CurrentPage = DirectCast(e.ClickedItem.Tag, TabPage)
    End Sub

    Private Sub DropdownMenu_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropdownMenu.VisibleChanged
        If (Not DropdownMenu.Visible) Then DropdownMenu.IsVisible = Me.ClientRectangle.Contains(Me.PointToClient(Control.MousePosition))
    End Sub

#End Region

#Region "Custom Menu Rendering"
    Class DropdownRenderer : Inherits ToolStripRenderer
        Public Theme As Color

        Public Sub New(ByVal theme As Color)
            Me.Theme = theme
        End Sub

        Protected Overrides Sub OnRenderToolStripBorder(ByVal e As System.Windows.Forms.ToolStripRenderEventArgs)
            Using pen As New Drawing.Pen(AddColor(Theme, -100))
                e.Graphics.DrawRectangle(pen, 0, 0, e.ToolStrip.Width - 1, e.ToolStrip.Height - 1)
            End Using
        End Sub

        Protected Overrides Sub OnRenderToolStripBackground(ByVal e As System.Windows.Forms.ToolStripRenderEventArgs)
            e.Graphics.Clear(AddColor(Theme, 100))
            Dim bounds As New Rectangle(0, 0, 26, e.ToolStrip.Height)
            Using brush As New Drawing2D.LinearGradientBrush(bounds, AddColor(Theme, 50), Theme, Drawing2D.LinearGradientMode.Horizontal)
                e.Graphics.FillRectangle(brush, bounds)
            End Using
        End Sub

        Protected Overrides Sub OnRenderMenuItemBackground(ByVal e As System.Windows.Forms.ToolStripItemRenderEventArgs)
            If (e.Item.Selected) Then
                e.Item.ForeColor = Color.Black
                Dim highlight As Color
                If (Theme.B > Theme.R AndAlso Theme.B > Theme.G) Then
                    highlight = ColorTranslator.FromHtml("#FFC580")
                ElseIf (Theme.G > Theme.R AndAlso Theme.G > Theme.B) Then
                    highlight = ColorTranslator.FromHtml("#FFFB80")
                Else
                    highlight = ColorTranslator.FromHtml("#A1C8FF") '"#D4EBFF")
                End If

                Using B As Drawing2D.LinearGradientBrush = New Drawing2D.LinearGradientBrush(New Rectangle(e.Item.ContentRectangle.Left, e.Item.ContentRectangle.Top, e.Item.ContentRectangle.Width / 2, e.Item.ContentRectangle.Height), Color.FromArgb(150, highlight), AddColor(highlight, 200), Drawing2D.LinearGradientMode.Horizontal)
                    B.WrapMode = Drawing2D.WrapMode.TileFlipXY
                    e.Graphics.FillRectangle(B, e.Item.ContentRectangle)
                End Using

                Using P As New Drawing.Pen(AddColor(highlight, -25))
                    e.Graphics.DrawRectangle(P, e.Item.ContentRectangle.Left, e.Item.ContentRectangle.Top, e.Item.ContentRectangle.Width - 1, e.Item.ContentRectangle.Height - 1)
                End Using
            Else
                e.Item.ForeColor = e.ToolStrip.ForeColor
                MyBase.OnRenderMenuItemBackground(e)
            End If
        End Sub
    End Class
#End Region
    
End Class